package test;

import entity.Catalog;
import jdbc.DataSourceUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;


import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.concurrent.BlockingQueue;

/**
 * Created by Zhangyu on 2014/8/1.
 */
public class KeywordsCrawl {
//  static LinkedList<String> list;

    private static DataSource dataSource;

    static {
        try {
            dataSource = DataSourceUtil.getDataSource();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }


    public void CrawlFood(){
        String url = "http://cd.meituan.com/category/meishi";
        MeituanCrawl.getcontentFromUrl(url);
    }

    public static void CrawlMovie(LinkedList<String>list){
        String url = "http://movie.douban.com/nowplaying/chengdu/";
        String content = MeituanCrawl.getcontentFromUrl((url));
        Document doc = Jsoup.parse(content);
        Elements eles = doc.getElementsByClass("stitle");
        for(int i = 0;i <eles.size();i++) {
            String keyword = new String(eles.get(i).select("a").attr("title"));
            System.out.println(keyword);
            try{
                if(true) {
                    list.add(keyword);
                }
            }catch (Exception e){
                System.err.println(e.getMessage());
            }
        }
    }

    public static void InsertIntoDB(String keyword) {
        Connection conn = null;
        PreparedStatement pstemt = null;
        if (!keyword.equals("")) {
            try {
                String sql = "INSERT INTO keywords(Kname) VALUES(\"" + keyword + "\");";
                conn = dataSource.getConnection();
                pstemt = conn.prepareStatement(sql);
                pstemt.executeUpdate();
                System.out.println("插入关键字\"" + keyword + "\"到表中");
            } catch (Exception e) {
                System.err.println("插入数据失败！" + e.getMessage());
            } finally {
                if (pstemt != null) {
                    try {
                        pstemt.close();
                    } catch (SQLException e) {
                        System.err.println(e.getMessage());
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        System.err.println(e.getMessage());
                    }
                }
            }
        }
        else
            System.out.println("关键字为空！");
    }

    public static void main(String args[]){
        LinkedList<String> list = new LinkedList<String>();
        CrawlMovie(list);
        String keyword;
        try {
            int i = 0;
            while (i < list.size()) {
                keyword = list.remove();
//              System.out.println(keyword);
                InsertIntoDB(keyword);
                i++;
            }
            System.out.println("更新关键字完成~");
        }catch (Exception e){
            System.err.println(e.getMessage());
        }
    }
}
